/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package uk.co.alt236.btlescan.adapter;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import uk.co.alt236.btlescan.ResourceTable;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

/**
 * ConnectItemProvider
 *
 * @author ljx
 * @since 2021-04-29
 */
public class DeviceItemProvider extends BaseItemProvider {
    private List<BluetoothLeDevice> list;
    private AbilitySlice slice;

    /**
     * DeviceItemProvider
     *
     * @param listData
     * @param abilitySlice
     */
    public DeviceItemProvider(List<BluetoothLeDevice> listData, AbilitySlice abilitySlice) {
        this.list = listData;
        this.slice = abilitySlice;
    }

    /**
     * setData
     *
     * @param listData
     */
    public void setData(List<BluetoothLeDevice> listData) {
        this.list = listData;
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return Optional.empty();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_sample, null, false);
        } else {
            cpt = convertComponent;
        }
        BluetoothLeDevice sampleItem = list.get(position);
        Text name = (Text) cpt.findComponentById(ResourceTable.Id_item_name);
        Text mac = (Text) cpt.findComponentById(ResourceTable.Id_mac);
        Text date = (Text) cpt.findComponentById(ResourceTable.Id_date);
        name.setText(sampleItem.getName());
        mac.setText(sampleItem.getAddress());
        Text rssi = (Text) cpt.findComponentById(ResourceTable.Id_rssi);
        long timestamp = sampleItem.getTimestamp();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(timestamp);
        date.setText(sd);
        rssi.setText(sampleItem.getRssi() + "db / " + sampleItem.getFirstRssi() + "db");
        return cpt;
    }
}